#include "iot_button.h"
#include "button_gpio.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "btn.h"
#include "led.h"

const char *TAG = "BTN";

button_handle_t gpio_btn = NULL;


void button_single_click_cb(void* handle, void* args) {
    ESP_LOGI(TAG, "Single click detected!");
    led_toggle();
}

void button_double_click_cb(void* handle, void* args) {
    ESP_LOGI(TAG, "Double click detected!");
}

void button_long_press_cb(void* handle, void* args) {
    ESP_LOGI(TAG, "Long press detected!");
}

void initBtns(){
    const button_config_t btn_cfg = {1000,300};
    const button_gpio_config_t btn_gpio_cfg = {
        .gpio_num = GPIO_NUM_9,
        .active_level = 0,
    };
    esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &gpio_btn);

    if(ret != ESP_OK){
        ESP_LOGE(TAG, "Button create failed");
    } else {
        ESP_LOGI(TAG, "Button create success");
        static button_info_t info = {.id = 1, .name = "Power"};
        button_event_args_t single_args = {0};
        iot_button_register_cb(gpio_btn, BUTTON_SINGLE_CLICK, &single_args,
                            button_single_click_cb, &info);


        button_event_args_t double_args = {
            .multiple_clicks= {
                .clicks = 2
            }
        };
        iot_button_register_cb(gpio_btn, BUTTON_MULTIPLE_CLICK, &double_args,
                            button_double_click_cb, &info);

                            
        button_event_args_t long_args = {
            .long_press = {
                .press_time = 1000
            }
        };
        iot_button_register_cb(gpio_btn, BUTTON_LONG_PRESS_START, &long_args,
                            button_long_press_cb, &info);
    }
}